package com.example.admin.yuejian21.fragment;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.admin.yuejian21.Adapter.TrainlistAdapter;
import com.example.admin.yuejian21.Bean.train;
import com.example.admin.yuejian21.R;
import com.example.admin.yuejian21.Service.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class PeixunFragment extends Fragment {

    private Button rumen,cainiao,jinjie,all;
    private FrameLayout train_frame;
    private FragmentTransaction transaction;
    private FragmentManager fragmentManager;
    private TrainAllFragment trainAllFragment;
    private TrainRumenFragment trainRumenFragment;
    private TrainCainiaoFragment trainCainiaoFragment;
    private TrainJinjieFragment trainJinjieFragment;
    private List<ImageView> images;
    private int[] imgResource={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four};
    private ViewPager train_vp;
    private View view;
    //当前图片的位置
    private int current;
    private Context context;
    //上一次图片的位置
    private int oldpos;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_peixun, container, false);
        init();
        event();
        lunbo();
        return view;
    }

    private void event() {
        jinjie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(trainJinjieFragment);
            }
        });
        rumen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(trainRumenFragment);
            }
        });
        cainiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(trainCainiaoFragment);
            }
        });
        all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(trainAllFragment);
            }
        });
    }

    private void lunbo() {
        train_vp.setAdapter(pageadapter);
    }

    public void  init(){
        rumen=(Button)view.findViewById(R.id.rumen);
        cainiao=(Button)view.findViewById(R.id.cainiao);
        jinjie=(Button)view.findViewById(R.id.jinjie);
        all=(Button)view.findViewById(R.id.all);
        train_frame=(FrameLayout)view.findViewById(R.id.train_frame);
        trainRumenFragment=new TrainRumenFragment();
        trainCainiaoFragment=new TrainCainiaoFragment();
        trainJinjieFragment=new TrainJinjieFragment();
        trainAllFragment=new TrainAllFragment();
        context=getActivity();
        fragmentManager=getChildFragmentManager();
        transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.train_frame,trainAllFragment);
        transaction.commit();
        train_vp=(ViewPager)view.findViewById(R.id.train_vp);

        //创建List对象，用于装载ImageView对象
        images=new ArrayList<ImageView>();
        for (int i=0;i<imgResource.length;i++)
        {
            ImageView imageView=new ImageView(getActivity());
            imageView.setBackgroundResource(imgResource[i]);
            images.add(imageView);
        }
    }

    PagerAdapter pageadapter=new PagerAdapter() {
        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position));
            return images.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position));
        }
    };
    class  PageTask implements Runnable{
        @Override
        public void run() {
            current=(current+1)%imgResource.length;
            handler.sendEmptyMessage(current);
        }
    }
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            train_vp.setCurrentItem(current);
        };
    };

    @Override
    public void onStart() {
        super.onStart();
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                current=(current+1)%imgResource.length;
                handler.sendEmptyMessage(current);
            }
        },2000,2000);

    }

    private void  replace(Fragment fragment)
    {
        transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.train_frame,fragment);
        transaction.commit();
    }
    private RecyclerView recyclerView;

}
